/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthSmallest = function (root, k) {
  let result = 0
  let rank = 0
  function bst(root, k) {
    if (root === null) {
      return
    }
    bst(root.left, k)
    rank++
    if (rank === k) {
      result = root.val
      return
    }
    bst(root.right, k)
  }
  bst(root, k)
  return result
}
